package com.dant.effects
{
	import com.dant.events.AdvancedPanelEvent;
	
	import flash.events.Event;
	
	import mx.core.mx_internal;
	import mx.effects.EffectManager;
	import mx.effects.effectClasses.TweenEffectInstance;

	use namespace mx_internal;

	public class DefaultAdvancedPanelActionInstance extends TweenEffectInstance
	{
		private var _widthFrom:Number;
		private var _widthTo:Number;
		private var _heightFrom:Number;
		private var _heightTo:Number;
		private var _xFrom:Number;
		private var _xTo:Number;
		private var _yFrom:Number;
		private var _yTo:Number;
		
		public function DefaultAdvancedPanelActionInstance(target:Object)
		{
			super(target);
		}

		public function get yTo():Number
		{
			return _yTo;
		}

		public function set yTo(value:Number):void
		{
			_yTo = value;
		}

		public function get yFrom():Number
		{
			return _yFrom;
		}

		public function set yFrom(value:Number):void
		{
			_yFrom = value;
		}

		public function get xTo():Number
		{
			return _xTo;
		}

		public function set xTo(value:Number):void
		{
			_xTo = value;
		}

		public function get xFrom():Number
		{
			return _xFrom;
		}

		public function set xFrom(value:Number):void
		{
			_xFrom = value;
		}

		public function get heightTo():Number
		{
			return _heightTo;
		}

		public function set heightTo(value:Number):void
		{
			_heightTo = value;
		}

		public function get heightFrom():Number
		{
			return _heightFrom;
		}

		public function set heightFrom(value:Number):void
		{
			_heightFrom = value;
		}

		public function get widthTo():Number
		{
			return _widthTo;
		}

		public function set widthTo(value:Number):void
		{
			_widthTo = value;
		}

		public function get widthFrom():Number
		{
			return _widthFrom;
		}

		public function set widthFrom(value:Number):void
		{
			_widthFrom = value;
		}

		override public function initEffect(event:Event):void
		{
			super.initEffect(event);

			if (event is AdvancedPanelEvent)
			{
				var advancedPanelEvent:AdvancedPanelEvent = AdvancedPanelEvent(event);
				if (isNaN(widthFrom))
				{
					widthFrom = advancedPanelEvent.oldWidth;
				}
				if (isNaN(widthTo))
				{
					widthTo = advancedPanelEvent.newWidth;
				}
				if (isNaN(heightFrom))
				{
					heightFrom = advancedPanelEvent.oldHeight;
				}
				if (isNaN(heightTo))
				{
					heightTo = advancedPanelEvent.newHeight;
				}

				if (isNaN(xFrom))
				{
					xFrom = advancedPanelEvent.oldX;
				}

				if (isNaN(xTo))
				{
					xTo = advancedPanelEvent.newX;
				}

				if (isNaN(yFrom))
				{
					yFrom = advancedPanelEvent.oldY;
				}

				if (isNaN(yTo))
				{
					yTo = advancedPanelEvent.newY;
				}
			}
		}

		override public function play():void
		{
			super.play();
			
			// Create a tween to resize and move the object
 			tween = createTween(this, [ widthFrom, heightFrom, xFrom, yFrom ],
											 [ widthTo, heightTo, xTo, yTo ], duration);
											 
			applyTweenStartValues();
			
 		}

		override public function onTweenUpdate(value:Object):void
		{
			super.onTweenUpdate(value);
 			
			EffectManager.suspendEventHandling();
			
			target.width = value[0];
			target.height = value[1];
			target.x = value[2];
			target.y = value[3];
			
			EffectManager.resumeEventHandling(); 
			
			trace ("TWEEN UPDATE - width: "+target.width+", height: "+target.height);
		}
	}
}